package com.egene.tw.data.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by wzx on 2018/7/1.
 */

public class DateFormUtils {
//    public static class DateValue {
//        public long time;
//        public String timeDesc;
//
//        public DateValue(long time, String timeDesc) {
//            this.time = time;
//            this.timeDesc = timeDesc;
//        }
//    }

//    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//    private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");

    public static final long HOUR = 1000 * 60 * 60;
    public static final long DAY = 1000 * 60 * 60 * 24;

    public static long oneDayStart(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(time);
        try {
            time = sdf.parse(dateStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    public static long oneMonthStart(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String dateStr = sdf.format(time);
        try {
            time = sdf.parse(dateStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    public static long oneWeekStart(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int index = calendar.get(Calendar.DAY_OF_WEEK);
        //1 日
        //2 yi
        switch (index) {
            case 1:
                time = time - (DAY * 6);
                break;
            case 2:
                time = time;
                break;
            case 3:
                time = time - DAY;
                break;
            case 4:
                time = time - (DAY * 2);
                break;
            case 5:
                time = time - (DAY * 3);
                break;
            case 6:
                time = time - (DAY * 4);
                break;
            case 7:
                time = time - (DAY * 5);
                break;
        }
        try {
            time = sdf.parse(sdf.format(time)).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    public static String weekDesc(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int index = calendar.get(Calendar.DAY_OF_WEEK);
        //1 日
        //2 yi
        String desc = "";
        switch (index) {
            case 1:
                desc = "週日";
                break;
            case 2:
                desc = "週一";
                break;
            case 3:
                desc = "週二";
                break;
            case 4:
                desc = "週三";
                break;
            case 5:
                desc = "週四";
                break;
            case 6:
                desc = "週五";
                break;
            case 7:
                desc = "週六";
                break;
        }
        return desc;
    }

    public static String weekBetwwDesc(long time) {
        time = oneWeekStart(time);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
        return sdf.format(time) + "~" + sdf.format(time + DAY * 7 - 1);
    }

    public static String weekBetwwDescTitle(long time) {
        time = oneWeekStart(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        return sdf.format(time) + "~" + sdf.format(time + DAY * 7 - 1);
    }

    ///////////////////////////////////////////////////////////////////////////
    //
    ///////////////////////////////////////////////////////////////////////////

//    public static DateValue parse(int year, int month, int day, int hour, int min) {
//        String ss = "20" + year + "-" + month + "-" + day + " " + hour + ":" + min;
//        long time = 0;
//        String timeDesc = "";
//        try {
//            time = sdf.parse(ss).getTime();
//            timeDesc = "";//sdf.format(time);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        return new DateValue(time, timeDesc);
//    }
//
//    public static long parse2Long(int year, int month, int day, int hour, int min) {
//        String ss = "20" + year + "-" + month + "-" + day + " " + hour + ":" + min;
//        long time = 0;
//        try {
//            time = sdf.parse(ss).getTime();
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        return time;
//    }

    public static long parse2Long(int year, int month, int day, int hour, int min) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, min);
        calendar.set(Calendar.HOUR_OF_DAY, hour);

        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.YEAR, 2000 + year);
        return calendar.getTimeInMillis();
    }


    ///////////////////////////////////////////////////////////////////////////
    //
    ///////////////////////////////////////////////////////////////////////////
    public static boolean isToday(long time){
        return DateFormUtils.oneDayStart(time) == DateFormUtils.oneDayStart(System.currentTimeMillis());
    }
    public static boolean isToWeek(long time){
        return DateFormUtils.oneWeekStart(time) == DateFormUtils.oneWeekStart(System.currentTimeMillis());
    }
    public static boolean isToMonth(long time){
        return DateFormUtils.oneMonthStart(time) == DateFormUtils.oneMonthStart(System.currentTimeMillis());
    }
}
